package com.shycloud.mido.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.music.domain.MisMusicBanner;
import com.shycloud.mido.music.domain.MisMusicCategory;
import com.shycloud.mido.music.domain.MisMusicRecommend;
import com.shycloud.mido.music.mapper.MisMusicBannerMapper;
import com.shycloud.mido.music.mapper.MisMusicCategoryMapper;
import com.shycloud.mido.music.mapper.MisMusicRecommendMapper;
import com.shycloud.mido.music.service.MisMusicCategoryService;
import com.shycloud.mido.music.vo.MusicCateNameVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 曲库分类 service 实现类
 *
 * @author nianhua.jiang
 * @date 2020/4/29 15:20
 */
@Service
@Slf4j
@AllArgsConstructor
public class MisMusicCategoryServiceImpl extends ServiceImpl<MisMusicCategoryMapper, MisMusicCategory> implements MisMusicCategoryService {

	/** 分类模块 Mapper */
	private MisMusicCategoryMapper categoryMapper;
	/** 分类模块 */
	private MisMusicRecommendMapper recommendMapper;
	/** Banner模块 */
	private MisMusicBannerMapper bannerMapper;

	/**
	 * 获取分类名称列表
	 *
	 * @param
	 * @return java.util.List<com.shycloud.mido.music.vo.MusicCateNameVO>
	 * @author nianhua.jiang
	 * @date 2020/4/29 15:20
	 */
	@Override
	public List<MusicCateNameVO> getCateNameList() {
		return categoryMapper.getCateNameList();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean deleteCategoryById(Integer categoryId) throws Exception {
		int result1;	int result2;	int result3;

		try {
			result1 = categoryMapper.deleteById(categoryId);
			result2 = recommendMapper.delete(
					new QueryWrapper<MisMusicRecommend>().eq("category_id", categoryId));
			result3 = bannerMapper.delete(
					new QueryWrapper<MisMusicBanner>().eq("category_id", categoryId));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new BusinessException("删除分类失败");
		}

		return result1 == 1 && result2 > 0 && result3 > 0 ? true : false;
	}

}
